{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e6e9077-301e-41cf-af0e-c853f8cb47d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "from math import floor\n",
    "\n",
    "import requests\n",
    "import zipfile\n",
    "import io\n",
    "\n",
    "from nv_ingest_client.client import NvIngestClient\n",
    "from nv_ingest_client.primitives import JobSpec\n",
    "from nv_ingest_client.primitives.tasks import ExtractTask\n",
    "from nv_ingest_client.primitives.tasks import SplitTask\n",
    "from nv_ingest_client.util.file_processing.extract import extract_file_content\n",
    "import logging, time\n",
    "\n",
    "# Download the pdfs if they aren't already present\n",
    "bo_20_path = \"./data/bo20/\"\n",
    "\n",
    "pdf_ids = [\n",
    "    \"1016445\",\n",
    "    \"1177640\",\n",
    "    \"1479052\",\n",
    "    \"1690009\",\n",
    "    \"2132230\",\n",
    "    \"1037700\",\n",
    "    \"1238975\",\n",
    "    \"1598224\",\n",
    "    \"2049749\",\n",
    "    \"2151932\",\n",
    "    \"1043219\",\n",
    "    \"1375277\",\n",
    "    \"1620834\",\n",
    "    \"2062555\",\n",
    "    \"2189929\",\n",
    "    \"1061225\",\n",
    "    \"1422550\",\n",
    "    \"1666072\",\n",
    "    \"2127440\",\n",
    "    \"2399488\",\n",
    "]\n",
    "\n",
    "if not os.path.exists(bo_20_path):\n",
    "    os.makedirs(bo_20_path)\n",
    "\n",
    "def get_zip_range(zip_id):\n",
    "    lower = floor(int(zip_id)/1000) * 1000\n",
    "    upper = lower + 999\n",
    "    return str(lower).zfill(4) + \"-\" + str(upper).zfill(4) + \"/\"\n",
    "\n",
    "\n",
    "for pdf_id in pdf_ids:\n",
    "    pdf_path = os.path.join(bo_20_path, pdf_id+\".pdf\")\n",
    "    if not os.path.exists(pdf_path):\n",
    "        print(f\"Downloading pdf: {pdf_id}.pdf. Note: this requires downloading a large zipfile so it may take a while\")\n",
    "        base_url = \"https://corp.digitalcorpora.org/corpora/files/CC-MAIN-2021-31-PDF-UNTRUNCATED/zipfiles/\"\n",
    "        zip_id = pdf_id[:4]\n",
    "        zip_range = get_zip_range(zip_id)\n",
    "        full_url = base_url + zip_range + zip_id + \".zip\"\n",
    "\n",
    "        if not os.path.exists(bo_20_path + \"temp\"):\n",
    "            os.makedirs(bo_20_path + \"temp\")\n",
    "        r = requests.get(full_url)\n",
    "        z = zipfile.ZipFile(io.BytesIO(r.content))\n",
    "        z.extractall(bo_20_path + \"temp\")\n",
    "\n",
    "        # Move desired file to bo_20 folder\n",
    "        os.rename(bo_20_path + \"temp/\" + pdf_id + \".pdf\", bo_20_path + pdf_id + \".pdf\")\n",
    "\n",
    "        # Delete excess pdfs\n",
    "        shutil.rmtree(bo_20_path + \"temp\", ignore_errors=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "385e5017-7def-4a48-a23b-8e3583c394f8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
